using System.IO;
using System.Text.RegularExpressions;

namespace UnityXEditor
{
    /// <summary>
    /// 编辑器文件工具
    /// </summary>
    public class CodeFileTool
    {
        /// <summary>
        /// 注释代码行
        /// </summary>
        /// <param name="fileName">带后缀的文件名</param>
        /// <param name="line">行完整字符串</param>
        /// <param name="enabled">是否启用</param>
        public static void CommentsLine(string fileName, string line, bool enabled)
        {
            string cspath = EditorFileTool.GetFilePath(fileName);
            if (cspath == null)
            {
                UnityEngine.Debug.LogError("注释代码行时，文件不存在：" + fileName);
                return;
            }
            string csfile = File.ReadAllText(cspath);
            if (enabled)
            {
                string newfile = Regex.Replace(csfile, @"(?<=[\s])//" +
                    line.Replace("(", @"\(").Replace(")", @"\)").Replace("[", @"\[").Replace("{", @"\{") + @"(?=[\s])",
                    line);
                if (newfile != csfile)
                {
                    File.WriteAllText(cspath, newfile);
                }
            }
            else
            {
                string newfile = Regex.Replace(csfile, @"(?<=[\s])" +
                    line.Replace("(", @"\(").Replace(")", @"\)").Replace("[", @"\[").Replace("{", @"\{") + @"(?=[\s])",
                    "//" + line);
                if (newfile != csfile)
                {
                    File.WriteAllText(cspath, newfile);
                }
            }
        }
        /// <summary>
        /// 注释代码行
        /// </summary>
        /// <param name="file">带后缀的文件名</param>
        /// <param name="lineBegin">行头部字符串</param>
        /// <param name="lineEnd">行尾部字符串</param>
        /// <param name="enabled">是否启用</param>
        public static void CommentsLine(string file, string lineBegin, string lineEnd, bool enabled)
        {
            string cspath = EditorFileTool.GetFilePath(file);
            if (cspath == null)
            {
                UnityEngine.Debug.LogError("注释代码行时，文件不存在：" + file);
                return;
            }
            string csfile = File.ReadAllText(cspath);
            if (enabled)
            {
                string newfile = Regex.Replace(csfile, @"(?<=[\s])//" +
                    lineBegin.Replace("(", @"\(").Replace(")", @"\)").Replace("[", @"\[").Replace("{", @"\{") +
                    @"([\s\S]*?)" +
                    lineEnd.Replace("(", @"\(").Replace(")", @"\)").Replace("[", @"\[").Replace("{", @"\{") +
                    @"(?=[\s])",
                    lineBegin + "$1" + lineEnd);
                if (newfile != csfile)
                {
                    File.WriteAllText(cspath, newfile);
                }
            }
            else
            {
                string newfile = Regex.Replace(csfile, @"(?<=[\s])" +
                    lineBegin.Replace("(", @"\(").Replace(")", @"\)").Replace("[", @"\[").Replace("{", @"\{") +
                    @"([\s\S]*?)" +
                    lineEnd.Replace("(", @"\(").Replace(")", @"\)").Replace("[", @"\[").Replace("{", @"\{") +
                    @"(?=[\s])",
                    "//" + lineBegin + "$1" + lineEnd);
                if (newfile != csfile)
                {
                    File.WriteAllText(cspath, newfile);
                }
            }
        }
        /// <summary>
        /// 替换代码行
        /// </summary>
        /// <param name="file">带后缀的文件名</param>
        /// <param name="line">行完整字符串</param>
        /// <param name="newText">新字符串</param>
        public static void ReplaceLine(string file, string line, string newText)
        {
            string cspath = EditorFileTool.GetFilePath(file);
            if (cspath == null)
            {
                UnityEngine.Debug.LogError("替换代码行时，文件不存在：" + file);
                return;
            }
            string csfile = File.ReadAllText(cspath);
            string newfile = Regex.Replace(csfile, @"(?<=[\s])" +
                line.Replace("(", @"\(").Replace(")", @"\)").Replace("[", @"\[").Replace("{", @"\{") +
                @"(?=[\s])",
                newText);
            if (newfile != csfile)
            {
                File.WriteAllText(cspath, newfile);
            }
        }
        /// <summary>
        /// 替换代码行
        /// </summary>
        /// <param name="file">带后缀的文件名</param>
        /// <param name="lineBegin">行头部字符串</param>
        /// <param name="lineEnd">行尾部字符串</param>
        /// <param name="newText">新字符串</param>
        public static void ReplaceLine(string file, string lineBegin, string lineEnd, string newText)
        {
            string cspath = EditorFileTool.GetFilePath(file);
            if (cspath == null)
            {
                UnityEngine.Debug.LogError("替换代码行时，文件不存在：" + file);
                return;
            }
            string csfile = File.ReadAllText(cspath);
            string newfile = Regex.Replace(csfile, @"(?<=[\s])" +
                lineBegin.Replace("(", @"\(").Replace(")", @"\)").Replace("[", @"\[").Replace("{", @"\{") +
                @"[\s\S]*?" +
                lineEnd.Replace("(", @"\(").Replace(")", @"\)").Replace("[", @"\[").Replace("{", @"\{") +
                @"(?=[\s])",
                newText);
            if (newfile != csfile)
            {
                File.WriteAllText(cspath, newfile);
            }
        }
    }
}